﻿using System.Collections;
using System.Collections.Generic;
using CoreEx.Common.Data;
using LinFu.IoC.Configuration;


namespace CoreEx.Common.Implementation.Data
{
    /// <summary>
    /// A generic tabular data structure.
    /// </summary>
    [Implements(typeof(ITable), LifecycleType.OncePerRequest)]
    public class Table : ITable
    {
        /// <summary>
        /// List that contains the rows that belongs to this table.
        /// </summary>
        private readonly IList<IRow> _rows = new List<IRow>();
        
        /// <summary>
        /// Dictionary that contains the columns that belongs to this table.
        /// </summary>
        private readonly Dictionary<string, IColumn> _columns = new Dictionary<string, IColumn>();


        /// <summary>
        /// Sets or gets the name of the table.
        /// </summary>
        public string TableName { get; set; }


        /// <summary>
        /// Gets an <see cref="IDictionary{TKey,TValue}"/> that contains the columns for this <see cref="ITable"/> instance.
        /// </summary>
        public IDictionary<string, IColumn> Columns
        {
            get { return _columns;}            
        }

        /// <summary>
        /// Gets an <see cref="IList{T}"/> that contains the rows for this <see cref="ITable"/> instance.
        /// </summary>
        public IList<IRow> Rows
        {
            get { return _rows; }
        }

        /// <summary>
        /// Gets an <see cref="IEnumerator{T}"/> used to enumerate over the rows in this table.
        /// </summary>
        /// <returns><see cref="IEnumerator{T}"/></returns>
        public IEnumerator<IRow> GetEnumerator()
        {
            return _rows.GetEnumerator();
        }

        /// <summary>
        /// Gets an <see cref="IEnumerator"/> used to enumerate over the rows in this table.
        /// </summary>
        /// <returns><see cref="IEnumerator"/></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}